package com.github.icloudpay.pay.core.rest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.icloudpay.pay.core.biz.PayChannelConfigBiz;
import com.github.icloudpay.pay.core.entity.PayChannelConfig;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.wxiaoqi.security.common.msg.ObjectRestResponse;
import com.github.wxiaoqi.security.common.msg.TableResultResponse;
import com.github.wxiaoqi.security.common.util.Query;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
@RequestMapping("admin/payChannelConfig")
public class PayChannelConfigController {

	@Autowired
	private PayChannelConfigBiz biz;
	
	/**
	 * 查询配置列表
	 * @param platformId
	 * @param merId
	 * @param page
	 * @param limit
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public TableResultResponse<PayChannelConfig> queryPayChannel(@RequestParam(value = "platformId") String platformId, 
			@RequestParam(value = "merId") String merId, 
			@RequestParam(value = "page", defaultValue = "1") Integer page, 
			@RequestParam(value = "limit", defaultValue = "10") Integer limit) {
		Map<String, Object> param = new HashMap<>();
		param.put("platformId", platformId);
		param.put("merId", merId);
		param.put("page", page);
		param.put("limit", limit);
		log.info("查询支付通道，请求参数。。。{}"+param);
		Query query = new Query(param);
		Page<Object> result = PageHelper.startPage(query.getPage(), query.getLimit());
		List<PayChannelConfig> list = biz.queryPayChannelConfig(param);
		log.info("查询支付通道，响应参数.。。。{}"+list);
		return new TableResultResponse<PayChannelConfig>(result.getTotal(), list);
	}
	
	/**
	 * 添加通道配置
	 * @param params
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public ObjectRestResponse<PayChannelConfig> addPayChannel(@RequestBody Map<String, Object> params) {
		log.info("添加支付通道配置，请求参数。。。{}"+params);
		PayChannelConfig config = new PayChannelConfig();
		String platformId = (String) params.get("platformId");
		String merId = (String) params.get("merId");
		String payChannelNoStr = (String) params.get("payChannelNo");
		String payChannelNo = payChannelNoStr.split("&")[0];
		String payChannelName = payChannelNoStr.split("&")[1];
		String payCompanyNo = payChannelNoStr.split("&")[2];
		String outMerNo = (String) params.get("outMerNo");
		String outSubMerNo = (String) params.get("outSubMerNo");
		String outMerAccount = (String) params.get("outMerAccount");
		String outMerKey = (String) params.get("outMerKey");
		String outMerPublicKey = (String) params.get("outMerPublicKey");
		String singType = (String) params.get("singType");
		String privateKeyPath = (String) params.get("privateKeyPath");
		String privateKeyPwd = (String) params.get("privateKeyPwd");
		config.setPlatformId(platformId);
		config.setMerId(merId);
		config.setPayChannelNo(payChannelNo);
		config.setPayChannelName(payChannelName);
		config.setPayCompanyNo(payCompanyNo);
		config.setOutMerNo(outMerNo);
		config.setOutSubMerNo(outSubMerNo);
		config.setOutMerAccount(outMerAccount);
		config.setOutMerKey(outMerKey);
		config.setOutMerPublicKey(outMerPublicKey);
		config.setSingType(singType);
		config.setPrivateKeyPath(privateKeyPath);
		config.setPrivateKeyPwd(privateKeyPwd);
		biz.insertSelective(config);
		return new ObjectRestResponse<PayChannelConfig>();
	}
	
	/**
	 * 更新通道配置
	 * @param params
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public ObjectRestResponse<PayChannelConfig> updatePayChannelConfig(@RequestBody Map<String, Object> params) {
		log.info("更新支付通道配置，请求参数。。。{}"+params);
		String outMerNo = (String) params.get("outMerNo");
		String outSubMerNo = (String) params.get("outSubMerNo");
		String outMerAccount = (String) params.get("outMerAccount");
		String outMerKey = (String) params.get("outMerKey");
		String outMerPublicKey = (String) params.get("outMerPublicKey");
		String singType = (String) params.get("singType");
		String privateKeyPath = (String) params.get("privateKeyPath");
		String privateKeyPwd = (String) params.get("privateKeyPwd");
		Integer id = (Integer) params.get("id");
		String payChannelNoStr = (String) params.get("payChannelNo");
		String payChannelNo = payChannelNoStr.split("&")[0];
		String payChannelName = payChannelNoStr.split("&")[1];
		String payCompanyNo = payChannelNoStr.split("&")[2];
		String platformId = (String) params.get("platformId");
		String merId = (String) params.get("merId");
		PayChannelConfig config = new PayChannelConfig();
		config.setPlatformId(platformId);
		config.setMerId(merId);
		config.setPayChannelNo(payChannelNo);
		config.setPayChannelName(payChannelName);
		config.setPayCompanyNo(payCompanyNo);
		config.setOutMerNo(outMerNo);
		config.setOutSubMerNo(outSubMerNo);
		config.setOutMerAccount(outMerAccount);
		config.setOutMerKey(outMerKey);
		config.setOutMerPublicKey(outMerPublicKey);
		config.setSingType(singType);
		config.setPrivateKeyPath(privateKeyPath);
		config.setPrivateKeyPwd(privateKeyPwd);
		config.setId(Long.valueOf(id));
		biz.updateById(config);
		return new ObjectRestResponse<PayChannelConfig>();
	}
	
	/**
	 * 详情
	 * @param params
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/info", method = RequestMethod.POST)
	public ObjectRestResponse<PayChannelConfig> queryPayChannelConfigInfo(@RequestBody Map<String, Object> params) {
		log.info("查询支付通道配置，请求参数.。。。{}"+params);
		ObjectRestResponse<PayChannelConfig> response = new ObjectRestResponse<>();
		List<PayChannelConfig> list = biz.queryPayChannelConfig(params);
		log.info("通道配置列表。。。{}"+list);
		if (list != null && list.size() > 0) {
			PayChannelConfig config = list.get(0);
			response.data(config);
		}
		log.info("查询支付通道配置，响应参数.。。。{}"+response);
		return response;
	}
	
	
	/**
	 * 删除
	 * @param params
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public ObjectRestResponse<PayChannelConfig> deletePayChannel(@RequestBody Map<String, Object> params) {
		log.info("删除支付通道，请求参数。。。{}"+params);
		PayChannelConfig config = new PayChannelConfig();
		String payChannelNo = (String) params.get("payChannelNo");
		String platformId = (String) params.get("platformId");
		String merId = (String) params.get("merId");
		Integer id = (Integer) params.get("id");
		config.setPayChannelNo(payChannelNo);
		config.setPlatformId(platformId);
		config.setMerId(merId);
		config.setId(Long.valueOf(id));
		biz.delete(config);
		return new ObjectRestResponse<PayChannelConfig>();
	}
	
}
